package com.whl.sm.service.impl.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.parking.ParkingRecordMapper;
import com.whl.sm.model.parking.BlackWhiteParkingList;
import com.whl.sm.model.parking.ParkingBill;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.model.parking.ParkingRecord;
import com.whl.sm.service.parking.BlackWhiteParkingListService;
import com.whl.sm.service.parking.ParkingBillService;
import com.whl.sm.service.parking.ParkingLotsService;
import com.whl.sm.service.parking.ParkingRecordService;
import com.whl.sm.service.system.OrgUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-01
 */
@Slf4j
@Service
public class ParkingRecordServiceImpl extends ServiceImpl<ParkingRecordMapper, ParkingRecord> implements ParkingRecordService {


    @Autowired
    private ParkingLotsService parkingLotsService;

    @Autowired
    @Lazy
    private ParkingBillService parkingBillService;

    @Autowired
    private BlackWhiteParkingListService blackWhiteParkingListService;

    @Autowired
    private OrgUserService orgUserService;
    /*
    * operate--1:进场0:离场
    * */
    @Override
    public Result addRecord(String parkingLotsId, String licensePlate, int operate) {
        try{
            if (operate == 1){
                int blackCount = blackWhiteParkingListService.count(new QueryWrapper<BlackWhiteParkingList>()
                        .eq("license_plate",licensePlate)
                        .eq("visible",1)
                        .eq("list_type",0)
                );
                if (blackCount >0) {
                    return Result.build(444,"车辆已被列入黑名单!");
                }

            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("license_plate",licensePlate);
            queryWrapper.eq("parking_lots_id",parkingLotsId);
            queryWrapper.eq("visible",1);
            List<ParkingRecord> hasList = list(queryWrapper);
            int hasCunt = hasList.size();
            if (hasCunt>0 && operate ==1){
                return Result.build(444,"车辆已经场!");
            }else if (operate ==0 && hasCunt == 0){
                return Result.build(444,"车辆未没有入场记录!");
            }
            if (operate ==1){
                ParkingRecord insertRecord = new ParkingRecord();
                insertRecord.setLicensePlate(licensePlate);
                insertRecord.setEnterTime(new Date());
                insertRecord.setParkingLotsId(parkingLotsId);
                getBaseMapper().insert(insertRecord);
            }else {
                ParkingRecord updateRecord = hasList.get(0);
                Result leaveRecord = leaveParking(updateRecord);
                if (leaveRecord.getCode() != 200){
                    return leaveRecord;
                }
            }
        }catch (Exception e){
            log.error("停车场记录管理---->手动操作失败："+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    public Result leaveParking(ParkingRecord parkingRecord){
        ParkingLots relateParking = parkingLotsService.getById(parkingRecord.getParkingLotsId());
        int count = blackWhiteParkingListService.count(new QueryWrapper<BlackWhiteParkingList>()
                .eq("license_plate",parkingRecord.getLicensePlate())
                .eq("visible",1)
                .eq("list_type",1)
        );
        if (count == 0 && relateParking.getExpenseId() != null){
            int countBill = parkingBillService.count(new QueryWrapper<ParkingBill>().eq("parking_record_id",parkingRecord.getId()));
            if (countBill == 0){
                return Result.build(444,"请先行支付停车费再离场！");
            }
        }
        parkingRecord.setLeaveTime(new Date());
        parkingRecord.setVisible(0);
        getBaseMapper().updateById(parkingRecord);
        return Result.ok();
    }

}
